﻿using System.Collections.Generic;
using System.Web.Mvc;
using SW.Lite.BLL;
using SW.Lite.Core.Attributes;
using SW.Lite.Core.Authority;
using SW.Lite.Infrastructure.Common;
using SW.Lite.Model;

namespace SW.Lite.WebSite.Areas.Master.Controllers
{
    /// <summary>
    /// Class ModelController.
    /// </summary>
    public class ModelController : BaseController
    {
        /// <summary>
        /// Indexes this instance.
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Menu(MenuCode = "ModelManage")]
        public ActionResult Index(int page)
        {
            var list = BLL.Model.All(page, PageSize);
            ViewBag.RecordCount = list.AllCount;
            return View(list);
        }

        /// <summary>
        /// Adds the step1.
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Button(MenuCode = "ModelManage", ButtonCode = "Add")]
        public ActionResult AddStep1()
        {
            return View();
        }

        /// <summary>
        /// Adds the step1.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <param name="title">The title.</param>
        /// <param name="description">The description.</param>
        /// <returns>RedirectResult.</returns>
        [Button(MenuCode = "ModelManage", ButtonCode = "Add")]
        [HttpPost]
        public ActionResult AddStep1(string code, string title, string description)
        {
            var addModel = new ModelModel { Code = code, Title = title, Description = description };
            Session["AddModel"] = addModel;
            return RedirectToAction("AddStep2");
        }

        /// <summary>
        /// Adds the step2.
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Button(MenuCode = "ModelManage", ButtonCode = "Add")]
        public ActionResult AddStep2()
        {
            var addModel = Session["AddModel"] as ModelModel;
            if (addModel == null)
            {
                return RedirectToAction("AddStep1");
            }

            addModel.Fields = addModel.Fields ?? new List<FieldModel>();
            ViewBag.RecordCount = addModel.Fields.Count;
            return View(addModel.Fields);
        }

        /// <summary>
        /// Adds the field.
        /// </summary>
        /// <param name="code">The code.</param>
        /// <param name="title">The title.</param>
        /// <param name="type">The type.</param>
        /// <param name="length">The length.</param>
        /// <param name="codeTable">The code table.</param>
        /// <returns>JsonResult.</returns>
        [Button(MenuCode = "ModelManage", ButtonCode = "Add")]
        [HttpPost]
        public JsonResult AddField(string code, string title, FieldType type, int length, string codeTable)
        {
            var addModel = Session["AddModel"] as ModelModel;
            if (addModel == null)
                return new JsonResult { Data = new ExecuteResult { Result = false, Message = "模型已丢失，请返回重新添加！" } };

            addModel.Fields.Add(new FieldModel { ModelID = addModel.Id, Code = code, Title = title, Type = (int)type, Length = length, CodeTable = codeTable });
            return new JsonResult { Data = new ExecuteResult { Result = true } };
        }

        /// <summary>
        /// Adds the model.
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Button(MenuCode = "ModelManage", ButtonCode = "Add")]
        [HttpPost]
        public JsonResult AddModel()
        {
            var addModel = Session["AddModel"] as ModelModel;
            if (addModel == null)
                return new JsonResult { Data = new ExecuteResult { Result = false, Message = "模型已丢失，请返回重新添加。" } };

            if (BLL.Model.Exists(addModel.Code))
                return new JsonResult { Data = new ExecuteResult { Result = false, Message = "模型已存在。" } };

            var result = BLL.Model.Insert(addModel);

            if (result)
                BLL.Model.CreateTable(addModel);

            return new JsonResult { Data = result };
        }

        /// <summary>
        /// Adds the step3.
        /// </summary>
        /// <returns>ActionResult.</returns>
        [Button(MenuCode = "ModelManage", ButtonCode = "Add")]
        public ActionResult AddStep3()
        {
            var addModel = Session["AddModel"] as ModelModel;
            if (addModel == null)
                return RedirectToAction("AddStep1");

            return View(addModel);
        }
    }
}